package blog.open1111.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import blog.open1111.entity.Manager;
import blog.open1111.util.Md5Util;
import blog.open1111.util.ResponseUtil;
import net.sf.json.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import blog.open1111.service.ManagerService;

/**
 * 管理员Controller类
 * @author user
 *
 */
@Controller
@RequestMapping("/manager2")
public class ManagerController {

	@Resource
	private ManagerService managerService;


	@RequestMapping("/login")
	public String login(Manager manager, HttpServletResponse response ) throws Exception{
		Subject subject = SecurityUtils.getSubject();
		String md5 = Md5Util.md5(manager.getPassword(), Md5Util.SALT);
		System.out.println("==============================="+md5);
		UsernamePasswordToken token = new UsernamePasswordToken(manager.getUserName(), md5);
		JSONObject result = new JSONObject();


		try{
			subject.login(token);
			result.put("success",true);
		}catch (Exception e){
			e.printStackTrace();
			result.put("success",false);
			result.put("errorInfo","用户名或密码错误");

		}
		ResponseUtil.write(response,result);
		return  null;
	}

}
